/**
 * \file main.c
 * \brief 
 * \author Somayya ELMATA
 * \author Theo BRASSEUR
 * \version 0.1
 * \date 25/12/2013
 *
 * Ce fichier main pour l'affichage sur la Consols
 *
 */
 

#ifndef UICONSOLE
#define UICONSOLE
#include "interface-console.h"
#endif

#ifndef TSM
#define TSM 
#include "tsm.h"
#endif

#ifndef ALGOGEN
#define ALGOGEN
#include "algo-genetique.h"
#endif

int main()
{
	
	FILE* file = NULL;
	City *city = NULL;
	int depart = -1;
	Population population = NULL;
	Population *tmp = NULL;
	int crit=0,nbrcity = 0, quota = 0, taille = 0,generation=0,x=0,met_sel=0;
	char choix = 0; 
	float proba_mut = 0.0;
	float proba_cro = 0.0;
	double seuil=0.0f;

	switch( acceuil() )
	{
		case 1:	
			load_file(&city, &nbrcity); 
			break;
		
		case 2:	
			exit(0);
			break;
		
		default: 	
			printf("Erreur Acceuil\n");
			exit(0);
			break;
	}
	
	do
	{
		switch(choix = menu())
		{
			case '1':
					print_barre();
					print_all_city(city, nbrcity);
					printf("\n\nTapez Entez pour revenir au Menu :");getchar();getchar();
					break;
				
			case '2':	
					depart = select_starting_city(city, nbrcity);
					break;
				
			case '3':	
					print_barre();
					print_matrix(city, compute_all_dst(city, nbrcity), nbrcity);
					printf("\n\nTapez Entez pour revenir au Menu :");getchar();getchar();
					break;
				
			case '4':
				if(city != NULL)
					free(city);
				if(population != NULL)
					free_population(population);
				load_file(&city, &nbrcity);
				break;
				
			case '5': 
					if(depart == -1)
					{
						printf("Vous n'avez pas encore choisi de ville de depart\n");
						printf("Appuyer sur entrer pour revenir au menu:");
						getchar();getchar();
					}
						
					else
					{
						file = fopen ("resultat","w");// pour créer ou ecraser le fichier
						fclose(file);
						print_barre();
						recup_parametre(&crit,&seuil,&taille, &quota, &met_sel, &proba_cro, &proba_mut, &generation);
						//crit=0;seuil=500;taille =20 ;quota=15; met_sel=0; proba_cro=0.1; proba_mut=0.1; generation=15;
						population = init_population(taille, city, nbrcity, city[depart]);
						print_barre();
						printf("La population de depart :\n");
						file = fopen ("resultat","a");
						fprintf(file,"Les parametre de lancement \n-----------------------------\n");
						fprintf(file,"La taille de la population: %d\n",taille);
						fprintf(file,"Le quota de selection: %d\n",quota);
						fprintf(file,"La methode de selection (0:TOURNOI   1:ELITISE ) : %d\n",met_sel);
						fprintf(file,"La probabilité de croisement : %f\n",proba_cro);
						fprintf(file,"La probabilité de mutation : %f\n",proba_mut);
						fprintf(file,"Le nombre de generation: %d\n------------------------------\n\n",generation);
						fprintf(file,"La population de depart :\n");
						fclose(file);
						print_population(population);
						printf("\n\nTapez Entez pour commencer l'algo genetique :");getchar();getchar();
						x=generation;

						do
						{
							print_barre();
							evaluation(population);
							tmp=selection(&population, quota-(x-generation), met_sel, nbrcity);
							population=*tmp;
							population = mutation(population, proba_mut);
							croisement(population, proba_cro);
							printf("La population de la nouvelle generation %d: \n\n", x-generation);
							file = fopen ("resultat","a");
							fprintf(file,"\n-------------------------------------------------------\n");
							fprintf(file,"La population de la nouvelle generation %d: \n\n", x-generation);
							fclose(file);
							print_population(population);
							//if(crit==1) // pour ne pas deminuer la selection en mode seuil
							generation--;
							
						}while( generation > 0 && seuil <= compute_path(plus_court_chemin(population)));
						printf("\nLe plus court chemin trouvé :\n");print_path(plus_court_chemin(population));
						printf("\n\nTapez Entez pour revenir au Menu :");getchar();
						if(population != NULL)
							free_population(population);
					}

					break;
				
				case 'q': break; 
				
				default: printf("Erreur Menu\n");
					exit(0);
				break;
		}
		
	}while(choix != 'q');
	system("clear");
	
	if(city != NULL)
		free(city);

	if(file != NULL)
		close(file);
	
	return 0;
}


